package cn.ttxs.lib.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import cn.ttxs.lib.R;
import cn.ttxs.lib.tool.ToolEmpty;
import cn.ttxs.lib.wiget.LollipopFixedWebView;


/**
 * action:WebView
 * Created by bsh_kt on 2017/10/16.
 * des:
 */
public class WebViewActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_back;
    private TextView tv_left_title;
    private ImageView iv_more;
    private LollipopFixedWebView webView;


    private String title;
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_left_title = (TextView) findViewById(R.id.tv_left_title);
        iv_more = (ImageView) findViewById(R.id.iv_more);
        webView = findViewById(R.id.webView);
        //设置 缓存模式
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);
        //适应屏幕
        webView.getSettings().setUseWideViewPort(true);
        //设置支持多点缩放
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);//隐藏Zoom缩放按钮
        //添加js接口
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                view.loadUrl(url);
                return true;
            }
        });

        //js中可通过window.androidJsInterface.getAppParams()来获取参数
        webView.addJavascriptInterface(new AndroidJsInterface(this), "androidJsInterface");
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress >= 100) {
                    dismissLoading();
                }
            }
        });
        showLoading(WebViewActivity.this);
        if (ToolEmpty.isNotEmpty(url)) {
            webView.loadUrl(url);
        } else {
            webView.loadUrl("http://www.xseii.com.cn/");
        }

        tv_left_title.setText(title);
        iv_back.setOnClickListener(this);
        iv_more.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.iv_back) {
            finish();
        } else if (i == R.id.iv_more) {//分享
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");// setType("audio/*");
            intent.putExtra(Intent.EXTRA_SUBJECT, "share");
            intent.putExtra(Intent.EXTRA_TEXT, "来自梯知汇的分享：" + url);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(Intent.createChooser(intent, getTitle()));
        }
    }

    /**
     * js提供的接口
     */
    class AndroidJsInterface {
        private Context mContext;

        public AndroidJsInterface(Context context) {
            this.mContext = context;
        }

        @JavascriptInterface
        public String getAppParams() {
            return "1234";
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return false;
    }
}
